package com.huahua.api.service.impl;

import com.huahua.api.entity.param.UnicodeParam;
import com.huahua.api.exception.CommonException;
import com.huahua.api.service.UnicodeService;
import com.huahua.api.util.CheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.text.UnicodeUtil;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

/**
 * Unicode编码转换服务实现
 */
@Slf4j
@Service
public class UnicodeServiceImpl implements UnicodeService {

    @Override
    public Mono<String> convert(UnicodeParam param) {
        return Mono.fromCallable(() -> {
            CheckUtil.checkStrNotBlank(param.getContent(), new CommonException("转换内容不能为空"));
            CheckUtil.checkStrNotBlank(param.getType(), new CommonException("转换类型不能为空"));

            String result;
            switch (param.getType()) {
                case "toUnicode":
                    result = UnicodeUtil.toUnicode(param.getContent());
                    break;
                case "fromUnicode":
                    result = UnicodeUtil.toString(param.getContent());
                    break;
                default:
                    throw new CommonException("不支持的转换类型，请使用toUnicode或fromUnicode");
            }

            log.debug("Unicode转换完成，类型：{}，结果长度：{}", param.getType(), result.length());
            return result;
        });
    }
}
